package com.ccsu.mymarket.controller.utils;

import com.ccsu.mymarket.entity.Catagory;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DataUtils {
    public <T> List<T> buildTree(List<T> nodes,String className,String idName,String parentIdName){
        try {
            Class clazzClass=Class.forName(className);
            Field idField=clazzClass.getField(idName);
            Field pidField=clazzClass.getField(parentIdName);
            idField.setAccessible(true);
            pidField.setAccessible(true);

            List<T> parentNodes=new ArrayList<T>();
//            过滤父节点
            for (T node:nodes){
                Integer id=(Integer)idField.get(node);
                Integer pid=(Integer)pidField.get(node);
                if (pid<0){
                    parentNodes.add(node);
                    nodes.remove(node);
                }
            }
        }catch (SecurityException e){

        }catch (NoSuchFieldException e){

        }catch (ClassNotFoundException e){

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

        public static List<Catagory> getCatagTree(List<Catagory> nodes) {

            Map<Integer, List<Catagory>> sub = nodes.stream().filter(node -> node.getParentCatag()>0)
                    .collect(Collectors.groupingBy(node -> node.getParentCatag()));

            nodes.forEach(node -> node.setChildren(sub.get(node.getId())));
            return nodes.stream().filter(node -> node.getParentCatag() < 0).collect(Collectors.toList());
        }


    /**
     * mod:1 -获取日期+时间 ； 2- 获取年月日 ； 3- 获取年月
     * */
    public static String getDateTime(int mod){
        String datetime=null;
        SimpleDateFormat tempDate=null;
        switch (mod){
            case 1:
                tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                datetime = tempDate.format(new java.util.Date());
                break;
            case 2:
                tempDate = new SimpleDateFormat("yyyy-MM-dd");
                datetime = tempDate.format(new java.util.Date());
                break;
            case 3:
                tempDate = new SimpleDateFormat("yyyy-MM");
                datetime = tempDate.format(new java.util.Date());
                break;
        }
        return datetime;
    }

        /**
         * 对象转Map
         * @param object
         * @return
         * @throws IllegalAccessException
         */
        public static Map beanToMap(Object object) throws IllegalAccessException {
            Map<String, Object> map = new HashMap<String, Object>();
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(object));
            }
            return map;
        }

        /**
         * map转对象
         * @param map
         * @param beanClass
         * @param <T>
         * @return
         * @throws Exception
         */
        public static <T> T mapToBean(Map map, Class<T> beanClass) throws Exception {
            T object = beanClass.getDeclaredConstructor().newInstance();
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                field.setAccessible(true);
                if (map.containsKey(field.getName())) {
                    field.set(object, map.get(field.getName()));
                }
            }
            return object;
        }

    /**
     * 将传入的数值进行四舍五入操作
     * 保留两位小数
     * @param num
     * @return
     */
        public static String roundOffDouble(Double num){
            DecimalFormat formater = new DecimalFormat("0.00");
            return formater.format(num);
        }

}
